package org.example.booksmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.booksmanagementsystem.constant.Constants;
import org.example.booksmanagementsystem.enums.ResultStatus;
import org.example.booksmanagementsystem.model.*;
import org.example.booksmanagementsystem.service.SpecialDealAdminService;
import org.example.booksmanagementsystem.service.SpecialDealNormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/specialNormal")
@RestController
public class SpecialDealNormalController {
    @Autowired
    private SpecialDealNormalService specialDealNormalService;

    @RequestMapping("/getSpecialListByPage")
    public Result addSpecialBookInfo(PageRequest pageRequest, HttpSession session){
        log.info("Controller--特价秒杀展示");

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            Result result = new Result();
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        PageResult<BookInfo> pageResult = null;

        try{
            pageResult = specialDealNormalService.getSpecialBookListByPage(pageRequest);
        }catch(Exception e){
            log.error("查询翻页信息错误：" + e);
        }

        return Result.success(pageResult);

    }

    @RequestMapping("/buy")
    public Result buy(Integer bookId, HttpSession session){
        Result result = new Result();
        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        boolean b = specialDealNormalService.buy(bookId,userInfo.getId());
        if(b==true){
            result.setStatus(ResultStatus.SUCCESS);
            return result;
        }

        result.setStatus(ResultStatus.FAIL);
        return result;

    }

    @RequestMapping("/batchPurchaseSpecialBook")
    public Result batchPurchaseSpecialBook(@RequestParam("idList") List<Integer> idList , HttpSession session){
        log.info("----------------------batchPurchaseSpecialBook");
        Result result = new Result();

        if(idList==null||idList.size()==0){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }
        try {
            for (Integer id : idList) {
                buy(id, session);
            }
        }catch (Exception e){
            log.info("批量购买错误："+ e.getMessage());
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);
        return result;

    }

    @RequestMapping("/noBuy")
    public Result noBuy(Integer bookId, HttpSession session){
        Result result = new Result();

        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        boolean b = specialDealNormalService.noBuy(bookId,userInfo.getId());

        if(b==true){
            result.setStatus(ResultStatus.SUCCESS);
            return result;
        }else{
            result.setStatus(ResultStatus.FAIL);
            return result;
        }
    }

    @RequestMapping("/getSpecialBookListOfNormalByPage")
    public Result getSpecialBookListOfNormalByPage(PageRequest pageRequest, HttpSession session){
        log.info("Controller--特价购物车展示");
        Result result = new Result();

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            result = new Result();
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        PageResult<BookInfo> pageResult = null;

        try{
            pageResult = specialDealNormalService.getSpecialShoppingtrolleyByPage(pageRequest,userInfo.getId());
        }catch(Exception e){
            log.error("查询翻页信息错误：" + e);
        }

        return Result.success(pageResult);
    }

    @RequestMapping("/batchNoBuyBook")
    public Result batchNoBuyBook(@RequestParam("idList") List<Integer> idList, HttpSession session){
        Result result = new Result();
        if(idList==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        try{
            for(Integer id : idList){
                noBuy(id, session);
            }
        }catch(Exception e){
            log.info("特价购物车中批量取消购买失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);
        return result;
    }

}
